﻿/*
 * Copyright © 2020, Wuxi Safover Intelligent Technology Co., Ltd. All rights reserved.
 * 
 * 无锡安永智能科技有限公司版权所有，未经许可，不得用于商业用途
 * 
 * Visit: https://www.safover.com/ 
 */

using System;
using System.Diagnostics;

namespace Safover.LidarCollector.Managers
{
    internal class CountDownTimer : System.Timers.Timer
    {
        /// <summary>
        /// Multi Scan 模式下，当前模式已经执行时间，构造函数中启动计时器
        /// </summary>
        internal Stopwatch Stopwatch { get; private set; }

        /// <summary>
        /// Multi Scan 模式下，当前模式配置的总体执行时间
        /// </summary>
        internal TimeSpan AllowedTime { get; private set; }

        /// <summary>
        /// Multi Scan 模式下，当前模式配置执行结束倒计时
        /// </summary>
        internal int CountDownSeconds
        {
            get
            {
                var seconds = (int)(AllowedTime.TotalSeconds - Stopwatch.Elapsed.TotalSeconds);

                return seconds > 0 ? seconds : 0;
            }
        }

        internal bool IsScanStepTimeout => Stopwatch.Elapsed > AllowedTime;

        /// <summary>
        /// 创建倒计时参数状态记录
        /// </summary>
        /// <param name="allowedTime"></param>
        internal CountDownTimer(TimeSpan allowedTime) : base(1000)
        {
            AllowedTime = allowedTime;

            Stopwatch = new Stopwatch();
            Stopwatch.Start();
        }
    }
}
